import { useRef, useEffect, useState } from 'react'

const useTable = () => {
  const pageRef = useRef(null)
  const headerRef = useRef(null)
  const [height, setHeight] = useState(500)
  const getHeight = () => { // 获取表格高度
    if (!pageRef.current) return
    const height = pageRef.current.offsetHeight - headerRef.current.offsetHeight - 150
    setHeight(height)
  }
  useEffect(() => { // 监听窗口大小变化
    getHeight()
    window.addEventListener('resize', getHeight)
    return () => window.removeEventListener('resize', getHeight)
  }, [])
  const handleTableData = () => {
    return new Array(100).fill(1).map((_, index) => {
      return {
        key: index + 1,
        name: 'SipSu小口酥',
        age: index + 18,
        address: `就点亮了星空 流浪的纸船忘不掉港口 渐灭的花火割不断彩虹 你就是我心中${index + 1}号`,
      }
    })
  }
  return {
    headerRef,
    pageRef,
    tableScroll: {
      x: 1000,
      y: height
    },
    handleTableData
  }
}

export default useTable